home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 2010 April / PCWorld0410.iso / hity wydania / Bank smakow / BankSmakow.air / BankSmakow.swf / scripts / com / makingwaves / sql / DatabaseManager.as next >
Text File  |  2009-12-16  |  5KB  |  146 lines

  1. package com.makingwaves.sql
  2. {
  3.    import com.makingwaves.config.Configuration;
  4.    import flash.data.SQLConnection;
  5.    import flash.data.SQLSchemaResult;
  6.    import flash.data.SQLTableSchema;
  7.    import flash.events.SQLErrorEvent;
  8.    import flash.filesystem.File;
  9.    import flash.utils.Dictionary;
  10.    import mx.core.Application;
  11.    
  12.    public class DatabaseManager
  13.    {
  14.       
  15.       private static var allowInstantiation:Boolean;
  16.       
  17.       private static var instance:DatabaseManager;
  18.        
  19.       
  20.       private var initComplete:Boolean = false;
  21.       
  22.       private var _connection:SQLConnection;
  23.       
  24.       public function DatabaseManager()
  25.       {
  26.          super();
  27.          if(allowInstantiation)
  28.          {
  29.             openDatabaseConnection();
  30.             return;
  31.          }
  32.          throw new Error("Use getInstance()");
  33.       }
  34.       
  35.       public static function getInstance() : DatabaseManager
  36.       {
  37.          if(instance == null)
  38.          {
  39.             allowInstantiation = true;
  40.             instance = new DatabaseManager();
  41.             allowInstantiation = false;
  42.          }
  43.          return instance;
  44.       }
  45.       
  46.       private function errorHandler(param1:SQLErrorEvent) : void
  47.       {
  48.          trace("Error code:",param1.error.errorID);
  49.          trace("Details:",param1.error.message);
  50.       }
  51.       
  52.       private function openDatabaseConnection() : void
  53.       {
  54.          var _loc1_:File = File.applicationStorageDirectory.resolvePath(Application.application.name + ".db");
  55.          _connection = new SQLConnection();
  56.          _connection.open(_loc1_);
  57.       }
  58.       
  59.       public function isSchemaComplete() : Boolean
  60.       {
  61.          var schema:SQLSchemaResult = null;
  62.          try
  63.          {
  64.             _connection.loadSchema(SQLTableSchema);
  65.             schema = _connection.getSchemaResult();
  66.             if(schema != null && schema.tables.length > 0)
  67.             {
  68.                return true;
  69.             }
  70.             return false;
  71.          }
  72.          catch(e:Error)
  73.          {
  74.             trace("Error retrieving schema");
  75.             return false;
  76.          }
  77.       }
  78.       
  79.       public function getAllRecipes() : Array
  80.       {
  81.          var _loc4_:Object = null;
  82.          var _loc5_:Array = null;
  83.          refreshSchema();
  84.          var _loc1_:Configuration = Configuration.getInstance();
  85.          var _loc2_:StatementExecutor = new StatementExecutor(_connection);
  86.          var _loc3_:Array = _loc2_.execute(_loc1_.getAllRecipesSQL,null).data;
  87.          for each(_loc4_ in _loc3_)
  88.          {
  89.             _loc5_ = getRecipeIngredients(_loc4_.id);
  90.             _loc4_.products = _loc5_;
  91.          }
  92.          return _loc3_;
  93.       }
  94.       
  95.       public function getRecipes(param1:String) : Array
  96.       {
  97.          var _loc6_:Object = null;
  98.          var _loc7_:Array = null;
  99.          this.refreshSchema();
  100.          var _loc2_:Configuration = Configuration.getInstance();
  101.          var _loc3_:StatementExecutor = new StatementExecutor(_connection);
  102.          var _loc4_:String = (_loc4_ = _loc2_.getRecipesSQL).replace(/:products/g,param1);
  103.          var _loc5_:Array = _loc3_.execute(_loc4_,null).data;
  104.          for each(_loc6_ in _loc5_)
  105.          {
  106.             _loc7_ = getRecipeIngredients(_loc6_.id);
  107.             _loc6_.products = _loc7_;
  108.          }
  109.          return _loc5_;
  110.       }
  111.       
  112.       public function close() : void
  113.       {
  114.          _connection.close();
  115.       }
  116.       
  117.       public function getCategories() : Array
  118.       {
  119.          var _loc1_:Configuration = Configuration.getInstance();
  120.          var _loc2_:StatementExecutor = new StatementExecutor(_connection);
  121.          return _loc2_.execute(_loc1_.getCategoriesSQL,null).data;
  122.       }
  123.       
  124.       public function getProducts() : Array
  125.       {
  126.          var _loc1_:Configuration = Configuration.getInstance();
  127.          var _loc2_:StatementExecutor = new StatementExecutor(_connection);
  128.          return _loc2_.execute(_loc1_.getProductsSQL).data;
  129.       }
  130.       
  131.       public function refreshSchema() : void
  132.       {
  133.          _connection.loadSchema();
  134.       }
  135.       
  136.       private function getRecipeIngredients(param1:int) : Array
  137.       {
  138.          var _loc2_:Configuration = Configuration.getInstance();
  139.          var _loc3_:StatementExecutor = new StatementExecutor(_connection);
  140.          var _loc4_:Dictionary;
  141.          (_loc4_ = new Dictionary())[":recipeId"] = param1;
  142.          return _loc3_.execute(_loc2_.getRecipeIngredientsSQL,_loc4_).data;
  143.       }
  144.    }
  145. }
  146.